package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oreilly.servlet.MultipartRequest;
import com.skyc.entity.Admin;
import com.skyc.service.AdminService;
import com.skyc.service.impl.AdminServiceImpl;
import com.skyc.util.FileUp;
import com.skyc.util.RenamePolicyCos;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/AdminSettingServlet")
public class AdminSettingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Origin", "http://admin.wenfan.com"); // 允许跨域请求
		response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
		response.setHeader("Access-Control-Allow-Headers", "Authorization");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");

		String op = request.getParameter("op");
		PrintWriter out = response.getWriter();

		if (op.equals("query")) {
			AdminService as = new AdminServiceImpl();
			Admin a = as.queryByid(1);
			System.out.println(a);
			String name = a.getAname();
			String phone = a.getAphone();
			String email = a.getAemail();
			String imgurl=a.getAimgurl();
			request.setAttribute("name", name);
			request.setAttribute("phone", phone);
			request.setAttribute("email", email);
			request.setAttribute("imgurl", imgurl);
			request.getRequestDispatcher("Admin_setting.jsp").forward(request, response);
		}

		if (op.equals("update")) {
			//1.获取文件上传的路径D:\lvse\apache-tomcat-8.5.13\webapps\News\myupload
			   //String savePath=request.getRealPath("/myupload");
			    String savePath="D:\\workspace\\Myeclipse2019\\Git\\项目\\bwj\\00_项目源代码+数据库\\SkyClothes\\WebContent\\images\\temp";//修改为本地地址
			   //2.设置文件上传相关的属性
			   int maxsize=1024*1024*100;   //字节
			   MultipartRequest req=new MultipartRequest(request, savePath, maxsize,"utf-8", new RenamePolicyCos());
			   //开始上传，上传成功返回文件名
			   String filename="";
			try {
				filename = FileUp.uploadfiles(req);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			AdminService as = new AdminServiceImpl();
			Admin a = new Admin();
			a.setAadminid(1);
			a.setAname(req.getParameter("user"));
			a.setAphone(req.getParameter("phone"));
			a.setAemail(req.getParameter("email"));
			String filename1=filename.replaceFirst("/", "");
			System.out.println(filename1);
			System.out.println(req.getParameter("phone"));
			a.setAimgurl(filename1);
			as.updateIUPE(a);
			out.print("<script>location='AdminSettingServlet?op=query';</script>");
		}

		if (op.equals("cpwd")) {
			AdminService as = new AdminServiceImpl();
			Admin a = as.queryByid(1);
			String cxpwd = a.getApwd();
			String ypwd = request.getParameter("ypwd");
			if (cxpwd.equals(ypwd)) {
				out.print("<script>location='Admin_modifypwd_step2.jsp';</script>");
			} else {
				out.print("<script>alert('密码错误');location='Admin_modifypwd_step1.jsp';</script>");
			}
		}

		if (op.equals("updatepwd")) {
			String  pwd = request.getParameter("pwd");
            String  newpwd = request.getParameter("newpwd");
              if(pwd.equals(newpwd)) {
			    AdminService as = new AdminServiceImpl();
			    Admin a = new Admin();
			    a.setAadminid(1);
			    a.setApwd(newpwd);
			    as.updatePWD(a);
			    out.print("<script>location='Admin_modifypwd_step3.jsp';</script>");
			} else {
				out.print("<script>alert('密码错误');location='Admin_modifypwd_step2.jsp';</script>");
			}
		}

		out.flush();
		out.close();
	}

}
